package org.itboys.basetemplate.web;

import org.apache.ibatis.annotations.Param;
import org.itboys.admin.entity.BusinessCenter;
import org.itboys.admin.entity.BusinessCenterUser;
import org.itboys.admin.service.BusinessCenterService;
import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.basetemplate.entity.ApplyInfo;
import org.itboys.basetemplate.service.ApplyInfoService;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * 申请信息管理
 * 
 * @author Administrator
 * 
 */

@RestController
@RequestMapping("/ApplyInfoController")
public class ApplyInfoController extends BaseController {

	@Autowired
	private ApplyInfoService applyInfoService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private BusinessCenterService businessCenterService;
	@Autowired
	private BusinessCenterUserService businessCenterUserService;

	@RequestMapping("/toVM")
	public ModelAndView toVM(Model model, HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/adve/applyInfo");
	}

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/list")
	public void list(@Param("status") Integer status, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
		sqlMap.put("centerId", user.getCenterId());
		if (status != null) {
			sqlMap.put("status", status);
		}
		Page<ApplyInfo> page = applyInfoService.pageQuery(sqlMap, request);
		for(ApplyInfo info : page.getData()) {
			BusinessCenter center = businessCenterService.getById(info.getCenterId());
			if(center != null) {
				info.setCenterName(center.getCompanyName());
			}
			Member member = memberService.getById(info.getMemberId());
			if(member != null) {
				info.setNickName(member.getNickname());
			}
		}
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}


	/**
	 * 审核
	 * 
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/verify")
	public void verify(@RequestParam Long id, @RequestParam Integer type, HttpServletRequest request, HttpServletResponse response) {
		try {
			applyInfoService.comfirm(id, type);
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			AjaxUtils.renderText(response, "fail");
		}
	}

//	@RequestMapping("/statisticsUser")
//	public ModelAndView statisticsUser(@RequestParam(required = false) Long id, Model model) {
//		model.addAttribute("id", id);
//		model.addAttribute("staticRoot", resourceHolder.getStringValue("staticRoot"));
//		return new ModelAndView("/shop/statisticsUser");
//	}
	
	/**
//	 * 统计收益
//	 * 
//	 * @param id
//	 * @param request
//	 * @param response
//	 * @throws IllegalAccessException
//	 * @throws InvocationTargetException
//	 */
//	@RequestMapping("/getStatisticsUser")
//	public void getStatisticsUser(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
//		Map<String, Object> resultMap = incomeRecordService.getUserProfit(id);
//		Statistics statistics = new Statistics();
//		BeanUtils.populate(statistics, resultMap);
//		List<Statistics> list = new ArrayList<Statistics>();
//		list.add(statistics);
//		JsonPageUtils.renderJsonPage(1L, list, response);
//	}

//	@RequestMapping("/getStatisticsUserByDate")
//	public void getStatisticsUserByDate(@RequestParam Long id, @Param("createTime") String createTime, @Param("finishedTime") String finishedTime, HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
//		Map<String, Object> resultMap = new HashMap<>();
//		resultMap.put("userId", id);
//		if (createTime != null && !"".equals(createTime)) {
//			resultMap.put("createTime", createTime + " 00:00:00");
//		}
//		if (finishedTime != null && !"".equals(finishedTime)) {
//			resultMap.put("finishedTime", finishedTime + " 23:59:59");
//		}
//		BigDecimal amount = incomeRecordService.getIncomeAmountByMap(resultMap);
//		List<IncomeRecord> list = incomeRecordService.findRecordDetails(resultMap);
//		if (list != null && list.size() > 0) {
//			IncomeRecord record = list.get(0);
//			record.setTotalAmount(amount.toString());
//		}
//		AjaxUtils.renderJson(response, list);
//	}
}
